package com.yyc.system.controller;

import com.yyc.core.Result;
import com.yyc.core.ResultGenerator;
import com.yyc.system.model.TCatalog;
import com.yyc.system.service.ITCatalogService;

import ch.qos.logback.core.rolling.helper.IntegerTokenConverter;
import tk.mybatis.mapper.entity.Condition;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yyc on 2019/01/03.
 */
@RestController
@RequestMapping("/t/catalog")
public class TCatalogController {
	@Autowired
	private ITCatalogService tCatalogService;

	@RequestMapping("/add")
	public Result add(@RequestBody TCatalog tCatalog) {
		tCatalogService.save(tCatalog);
		return ResultGenerator.genSuccessResult();
	}

	@RequestMapping("/delete")
	public Result delete(@RequestParam Integer id) {
		tCatalogService.deleteById(id);
		return ResultGenerator.genSuccessResult();
	}

	@RequestMapping("/update")
	public Result update(@RequestBody TCatalog tCatalog) {
		tCatalogService.update(tCatalog);
		return ResultGenerator.genSuccessResult();
	}

	@RequestMapping("/detail")
	public Result detail(@RequestParam Integer id) {
		TCatalog tCatalog = tCatalogService.findById(id);
		return ResultGenerator.genSuccessResult(tCatalog);
	}

	@RequestMapping("/list")
	public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
		PageHelper.startPage(page, size);
		List<TCatalog> list = tCatalogService.findAll();
		PageInfo<TCatalog> pageInfo = new PageInfo<TCatalog>(list);
		return ResultGenerator.genSuccessResult(pageInfo);
	}

	// ****************************************************************************

	@RequestMapping("/queryCatalogByBookId")
	public Result queryCatalogByBookId(Integer bookId) {
		List<TCatalog> list = tCatalogService.findCatalogByBookId(bookId);
		return ResultGenerator.genSuccessResult(list);
	}

	@RequestMapping("/queryAll")
    public Result queryAll(){
    	List<TCatalog> list = tCatalogService.findAll();
    	Map<String, List<TCatalog>> rs = new HashMap<>();
    	list.forEach(catalog->{
    		if(rs.get(catalog.getBookId().toString()) == null) {
    		List<TCatalog> items = new ArrayList<>();
			items.add(catalog);
			rs.put(catalog.getBookId().toString(), items);
    		}else {
    			rs.get(catalog.getBookId().toString()).add(catalog);
    		}
    	});
		return ResultGenerator.genSuccessResult(rs);
    }
	
	@RequestMapping("/queryCatalogByBookName")
	public Result queryCatalogByBookName(String catalogName) {
		Condition condition = new Condition(TCatalog.class);
		condition.createCriteria().andEqualTo("catalogName", catalogName);
		List<TCatalog> list = tCatalogService.findByCondition(condition);
		return ResultGenerator.genSuccessResult(list);
	}
}
